package com.sys.gradepaysys.controller.stuServlet;

import com.alibaba.fastjson.JSON;
import com.sys.gradepaysys.entity.ResultData;
import com.sys.gradepaysys.entity.Student;
import com.sys.gradepaysys.service.StudentService;
import com.sys.gradepaysys.service.StudentServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sNum = request.getParameter("sNum");
        String name = request.getParameter("sName");
        String password = request.getParameter("password");
        String grade = request.getParameter("grade");
        String clazz = request.getParameter("clazz");
        String tel = request.getParameter("tel");
        String sex = request.getParameter("sex");
        String qq = request.getParameter("qq");

        Student student = new Student();
        student.setsName(name);

        student.setsNum(sNum);
        student.setPassword(password);
        student.setgName(grade);
        student.setCname(clazz);
        student.setsTel(tel);
        student.setSex(sex);
        student.setQq(qq);
        System.out.println(student);
        boolean b = studentService.registerStu(student);

        String s = "";
        if (b) {
            s = JSON.toJSONString(new ResultData<>(200, "注册成功!", null));
        } else {
            s = JSON.toJSONString(new ResultData<>(200, "注册失败!", null));
        }
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
